﻿using CJAtech.BBS.DataEntity;
using CJAtech.BBS.Process;
using CJAtech.BBS.Web.Filters;
using CJAtech.BBS.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bigger.Framework;
using Bigger.Web.Helper;

namespace CJAtech.BBS.Web.Controllers
{
    [WXAuthorFilter(PowerType.CheckLogin)]
    public class WXBBSController : BBSController
    {
        public override UserInfo GetUserInfo()
        {
            return ViewBag.UserInfo as UserInfo;
        }

        [WXAuthorFilter(PowerType.None)]
        public ActionResult Target(string openId, string targetUrl)
        {
            if (targetUrl.IsNull())
            {
                return Http404();
            }
            var objCommunityConfig = HttpContext.GetItems("CommunityConfig") as CommunityConfig;
            InvokeResult objInvokeResult = objCommunityConfig.InterfaceUrl.Replace("{openId}", openId, System.Text.RegularExpressions.RegexOptions.IgnoreCase).CreateHttpGet<InvokeResult>();
            if (objInvokeResult.ResultCode != "Success" || objInvokeResult.Data == null)
            {
                return Http404();
            }
            CookieHelper.SetCookieValue(objInvokeResult.Data.JsonSerialize(), objCommunityConfig.ConfigCode + "WXToken");
            return Redirect(targetUrl);
        }
    }
}
